package ex.com.jm.food.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import ex.com.jm.food.entity.Attr;
import ex.com.jm.food.entity.SubType;
import ex.com.jm.food.service.AdminAttrService;
import ex.com.jm.food.utils.DateUtils;
import ex.com.jm.food.utils.JsonUtils;

public class AdminAttrAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	
	private AdminAttrService adminAttrService;
	private int initPage=1;
	private int pageRow=10;
	private String attrName;
	private Integer type;
	private String order;
	private String sort;
    private Integer attrId;
    private int toalCount;
    private Attr attr;
    private Integer subId;
    private List<SubType> subTypes;
	public AdminAttrService getAdminAttrService() {
		return adminAttrService;
	}
	public void setAdminAttrService(AdminAttrService adminAttrService) {
		this.adminAttrService = adminAttrService;
	}
	public int getInitPage() {
		return initPage;
	}
	public void setInitPage(int initPage) {
		this.initPage = initPage;
	}
	public int getPageRow() {
		return pageRow;
	}
	public void setPageRow(int pageRow) {
		this.pageRow = pageRow;
	}
	public String getAttrName() {
		return attrName;
	}
	public void setAttrName(String attrName) {
		this.attrName = attrName;
	}
	public Integer getType() {
		return type;
	}
	public void setType(Integer type) {
		this.type = type;
	}
	public String getOrder() {
		return order;
	}
	public void setOrder(String order) {
		this.order = order;
	}
	public String getSort() {
		return sort;
	}
	public void setSort(String sort) {
		this.sort = sort;
	}
	public Integer getAttrId() {
		return attrId;
	}
	public void setAttrId(Integer attrId) {
		this.attrId = attrId;
	}
	public int getToalCount() {
		return toalCount;
	}
	public void setToalCount(int toalCount) {
		this.toalCount = toalCount;
	}
	public Attr getAttr() {
		return attr;
	}
	public void setAttr(Attr attr) {
		this.attr = attr;
	}
	public Integer getSubId() {
		return subId;
	}
	public void setSubId(Integer subId) {
		this.subId = subId;
	}
	public List<SubType> getSubTypes() {
		return subTypes;
	}
	public void setSubTypes(List<SubType> subTypes) {
		this.subTypes = subTypes;
	}
	/**
	 * 到list列表页面
	 * @return
	 */
	public String attrList(){
		return "attrList";
	}
	/**
	 * 得到属性列表
	 */
	public void getAttrs(){
		 HttpServletResponse response = ServletActionContext.getResponse();  
		 response.setContentType("text/plain;charset=utf-8"); 
		 int offset=(initPage-1)*pageRow;
		 List<Attr> attrs=this.getAdminAttrService().getAttrs(offset, pageRow, attrName,type,subId,sort, order);
		 JSONArray jsonAttr=JSONArray.fromObject(attrs);
		 toalCount=this.getAdminAttrService().attrCounts(attrName, type,subId);
		 JSONObject jo=JsonUtils.buildJson(jsonAttr, initPage, pageRow,toalCount);
		 PrintWriter out=null;
			try {
				out = response.getWriter();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			out.print(jo);
			out.flush();
			out.close();
	}
	/**
	 * 获取属性信息/到达添加属性页面
	 * @return
	 */
	public String editAttr(){
		subTypes=this.getAdminAttrService().getSubTypes();
		System.err.println(subTypes.size());
		if(attrId!=null){
			attr=this.getAdminAttrService().editAttr(attrId);
			subId=attr.getSubId().getSubId();
		}
		return "edit";
	}
	/**
	 * 保存属性
	 * @return
	 */
	public String saveAttr(){
		if(attr.getAttrName()==null||attr.getAttrName().equals("")){
			addFieldError("attr.attrName", "属性名称不能为空");
			return "edit";
		}
		Attr checkName=this.getAdminAttrService().checkName(attr.getAttrName());
		if(checkName!=null&&attr.getAttrId()==null){
			addFieldError("attr.attrName", "属性名称已存在");
			return "edit";
		}
		if(attr.getAttrId()==null||attr.getAttrId().equals("")){
			attr.setCreated(DateUtils.getCurrentDate());
		}
		attr.setSubId(this.getAdminAttrService().getSubType(subId));
		if (attr.getAttrId()!=null&&!attr.getAttrId().equals("")) {
			this.getAdminAttrService().updateAttr(attr);
		}else{
			this.getAdminAttrService().saveAttr(attr);
		}
		return "attrList";
	}
	
	public void checkName(){
		HttpServletResponse response = ServletActionContext.getResponse();  
	    response.setContentType("text/plain;charset=utf-8");  
		Attr checkName=this.getAdminAttrService().checkName(attrName);
		String msg;
		if(checkName!=null&&attrId==null){
			msg="该用户名称已被注册！";;
		}else {
			msg="该用户名称可用！";
		}
		JSONArray jsonMsg=JsonUtils.buildMessage(msg);
		PrintWriter out=null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(jsonMsg);
	    out.print(jsonMsg);
	}
	/**
	 * 删除属性
	 * @return
	 */
	public String delAttr(){
		this.getAdminAttrService().delAttr(attrId);
		return "attrList";
		
	}
}
